import uvicorn
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from db import SessionLocal
from crud import query_address


app = FastAPI(
    title='五级联动接口文档',
    description='省市县镇村五级联动接口文档',
    version='1.0.0'
)

# 设置跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_methods=['*'],
    allow_headers=['*'],
    allow_credentials=True,
)


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


@app.get("/getAddress")
def get_address(code: str = '', db: SessionLocal = Depends(get_db)):
    return query_address(db, code)


if __name__ == '__main__':
    uvicorn.run('main:app', host='0.0.0.0', port=8080,
                reload=True, workers=1)
